/**
 * @description  超额风险趋势图
 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { SecondHeaderC, XYAxisChartsC } from '../../../../comps';
import style from '../index.less';

class ExcessTrend extends PureComponent {
  constructor() {
    super();
    this.state = {
      ExcessTrendData: {},
    };
  }
  componentDidMount() {
    const { dispatch, args } = this.props;
    this.getExcessTrend(args);
  }
  UNSAFE_componentWillReceiveProps(nextProps) {
    const { dispatch, args, timeStamp } = nextProps;
    if (JSON.stringify(timeStamp) !== JSON.stringify(this.props.timeStamp)) {
      this.setState({
        ExcessTrendData: {},
      });
      this.getExcessTrend(args);
    }
  }
  getExcessTrend = (args) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'riskAttributionM/getExcessTrend',
      payload: args,
      callback: (data) => {
        if (data) {
          let obj = { color: 'rgba(133, 142, 239, 0.5)' };
          if (data.series && data.series[2]) data.series[2].areaStyle = obj;
        }
        this.setState({
          ExcessTrendData: data || {},
        });
      },
    });
  };

  showCharts = () => {
    const { ExcessTrendData } = this.state;
    const xAxis = ExcessTrendData.xAxis;
    const series = ExcessTrendData.series;
    return (
      <div style={{ height: '340px' }}>
        <XYAxisChartsC xAxis={xAxis} series={series} height='330px' yAxisUnit={['单位(%)']} dataZoom={true} width='100%' />
      </div>
    );
  };

  render() {
    return (
      <div style={{ marginTop: '10px' }} className={style.back_charts}>
        <SecondHeaderC title='超额风险趋势图' />
        <div style={{ height: '340px' }} className='echarts'>
          {this.showCharts()}
        </div>
      </div>
    );
  }
}
export default connect(({ riskAttributionM }) => ({
  riskAttributionM,
}))(ExcessTrend);
